<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
  <TITLE>Tech Tips: September 24, 1997</TITLE>
  <META NAME="AUTHOR" CONTENT="Glen McCluskey">
  <META NAME="DESCRIPTION" CONTENT="This issue of the JDC Tech Tips deals with 
  printing and global variables.">
  <META NAME="CREATED" CONTENT="09/24/97">
  <META NAME="KEYWORDS" CONTENT=" ">
  <META NAME="OWNER" CONTENT="Editorial/JDC">
  <META NAME="revision" CONTENT="@(#)tt0924.src	1.8 06/04/99  JDC">
  <META NAME="TOPIC" CONTENT="tech tips, printing, programming, Java Development Kit">
  
  <STYLE TYPE="text/css">
    <!-- 
    CODE {font-family: Courier, Monospace;
          font-size: 12pt}
    PRE {font-family: Courier, Monospace;
         font-size: 11pt}
      -->
  </STYLE>
</HEAD>

<!-- Start Body Insert-->
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" ALINK="#FF0000" VLINK="#660066">
<!-- End Body Insert-->

<!-- Start PageTop Insert -->

<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0" WIDTH="100%">
  <TR ALIGN="CENTER" VALIGN="TOP">
    <TD WIDTH="157" ALIGN="LEFT">
    <IMG SRC="/images/pixel.gif" HEIGHT="40" WIDTH="40" ALT="">
    <A HREF="http://java.sun.com/index.html"><IMG SRC="/images/javalogo52x88.gif" WIDTH="52" HEIGHT="88" ALT="Java Technology Home Page" BORDER="0"></A>
    <BR>
    <IMG SRC="/images/pixel.gif" WIDTH="157" HEIGHT="1" ALT=""></TD>

    <TD>

    <FORM NAME="seek1" METHOD="GET" ACTION="http://search.java.sun.com/query.html">
    <TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" WIDTH="100%">
      <TR>
        <TD ALIGN="RIGHT">
        <IMG SRC="/images/stripelt.gif" WIDTH="6" HEIGHT="14" ALT=""></TD>
        
        <TD WIDTH="100%">
        
        <TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" WIDTH="100%">
          <TR>
            <TD BGCOLOR="#CC9966" WIDTH="100%">
            <IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
          </TR>
          
          <TR>
            <TD>
            <IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
          </TR>
          
          <TR>
            <TD BGCOLOR="#CC9966">
            <IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
          </TR>
          
          <TR>
            <TD>
            <IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
          </TR>
          
          <TR>
            <TD BGCOLOR="#CC9966">
            <IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
          </TR>
          
          <TR>
            <TD>
            <IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
          </TR>
          
          <TR>
            <TD BGCOLOR="#CC9966">
            <IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>
          </TR>
        </TABLE>
        </TD>
        
        <TD ALIGN="LEFT">
        <IMG SRC="/images/stripert.gif" WIDTH="6" HEIGHT="14" ALT=""></TD>

        <TD>
        
        <TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">
          <TR>
            <TD VALIGN="CENTER">
            <A HREF="http://java.sun.com/a-z/index.html"><IMG SRC="/images/azindex.gif" BORDER="0" WIDTH="72" HEIGHT="11" ALT="A-Z Index"></A></TD>
              
            <TD VALIGN="CENTER">
            <FONT FACE="Helvetica" SIZE="1">
            <INPUT TYPE="text" SIZE="15" MAXLENGTH="128" NAME=qt></FONT></TD>
            
            <TD VALIGN="CENTER">
            <INPUT TYPE="image" SRC="/images/search.button.gif" WIDTH="55" HEIGHT="14" ALT="Search" value="search" BORDER="0"></TD>
          </TR>
        </TABLE>
        </TD>
      </TR>
    </TABLE>
    </FORM>
    <P>

    <TABLE BORDER="0" WIDTH="100%" CELLPADDING="0" CELLSPACING="0">
      <TR VALIGN="TOP">
        <TD WIDTH="100%"><IMG SRC="/images/chiclet.row.gif" WIDTH="55" 
          HEIGHT="18" ALT=""></TD>
          
        <TD ROWSPAN="4" ALIGN="RIGHT" WIDTH="152">
        <A HREF="/developer/index.html"><IMG SRC="/images/developer.connection.header.gif" BORDER="0" HEIGHT="42" WIDTH="319" ALT="Java Developer Connection(SM)"></A></TD>
      </TR>
      
      <TR VALIGN="TOP">
        <TD BGCOLOR="#FFFFFF" HEIGHT="1" WIDTH="100%">
        <IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD>
      </TR>
      
      <TR VALIGN="TOP">
        <TD BGCOLOR="#CC9966" HEIGHT="1" WIDTH="100%">
        <IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD>
      </TR>
      
      <TR VALIGN="TOP">
        <TD><A HREF="/developer/TechTips/"><IMG SRC="/images/technical-tips.gif" WIDTH="165" HEIGHT="22" ALT="Technical Tips"  BORDER=0></A></TD>
      </TR>
    </TABLE>

    </TD>
  </TR>
</TABLE>


<!-- End PageTop Insert -->

<!-- Start NavBar Insert -->
<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="3" BGCOLOR="#FFFFFF" WIDTH="157" ALIGN="LEFT">

<!-- tab categories -->

<TR>
<TD><A HREF="http://java.sun.com/products/"><IMG SRC="/images/side.tab.products.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Downloads, APIs, Documentation"></A></TD>
</TR>

<TR>
<TD><A HREF="/developer/index.html"><IMG SRC="/images/side.tab.developer.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Java Developer Connection"></A></TD>
</TR>

<TR>
<TD><A HREF="/developer/infodocs/index.shtml"><IMG SRC="/images/side.tab.docs.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Tutorials, Tech Articles, Training"></A></TD>
</TR>

<TR>
<TD><A HREF="/developer/support/index.html"><IMG SRC="/images/side.tab.support.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Online Support"></A></TD>
</TR>

<TR>
<TD><A HREF="/developer/community/index.html"><IMG SRC="/images/side.tab.community.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Community Discussion"></A></TD>
</TR>

<TR>
<TD><A HREF="http://java.sun.com/industry/"><IMG SRC="/images/side.tab.news.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="News &amp; Events from Everywhere"></A></TD>
</TR>

<TR>
<TD><A HREF="http://java.sun.com/solutions"><IMG SRC="/images/side.tab.solutions.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Products from Everywhere"></A></TD>
</TR>

<TR>
<TD><A HREF="http://java.sun.com/casestudies"><IMG SRC="/images/side.tab.case.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="How Java Technology is Used Worldwide"></A></TD>
</TR>
<TR>
<TD>    <DIV ALIGN="RIGHT">
    <A HREF="/servlet/PrintPageServlet"><IMG SRC="/images/printbutton.gif" WIDTH="155" HEIGHT="25" ALT="Print Button" BORDER="0"></A>
    </DIV>
</TD>
</TR>

<TR><TD>&nbsp;</TD></TR>


<!-- End NavBar Insert -->


<!-- START SUB-NAV -->

<TR>
  <TD><!-- INSERT SUB-NAV INFO -->
  </TD>
</TR>


<!-- END SUB-NAV -->

</TABLE>

<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">
<TR>
<TD>

<!-- Template Version 2.0 -->

<!-- ================== -->
<!-- Start Main Content -->
<!-- ================== -->


<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">
<TR>
  <TD VALIGN="TOP">
  <FONT FACE="Verdana, Arial, Helvetica, sans-serif">
    
    <DIV ALIGN="RIGHT">
    <FONT SIZE="-1"><A HREF="../index.html">Tech Tips index</A></FONT><BR>
    <H2>Tech Tips<BR>
    <FONT SIZE="4">September 24, 1997</FONT></H2></DIV>

<P>
This issue presents tips, techniques, and sample code for the following topics:

<P>
<UL>
  <LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
  <A HREF="#tip1">Printing</A></FONT>
  <LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif">
  <A HREF="#tip2">Global Variables</A></FONT>
</UL>

<P>
<A NAME="tip1"></A>
<STRONG>Printing in Java</STRONG><BR>
For printing in Java, try this. JDK<FONT SIZE="-2"><SUP>TM</SUP></FONT> 1.1 
provides a standard way to print and improves on JDK 1.0 printing.

<P>
You print via the Abstract Window Toolkit (AWT). A simple example for
printing a filled-in oval looks like this:

<P>
</FONT>

<PRE>
<CODE>
import java.awt.*;

public class print {

  public static void main(String args[])
  {
    Frame f = new Frame("test");
    f.pack();

    PrintJob pj =
      f.getToolkit().getPrintJob(f, "print1", null);

    if (pj != null) {
      Graphics g = pj.getGraphics();

      g.fillOval(5, 5, 150, 100);

      g.dispose();

      pj.end();
    }
    System.exit(0);
  }
}
</CODE>
</PRE>

<FONT FACE="Verdana, Arial, Helvetica, sans-serif">

<P>   
This example is a standalone Java program that creates an AWT frame, a
top-level GUI component for running an application. The program next
retrieves the toolkit for the frame. The toolkit for the AWT is the interface
between the abstract window layer and an actual windowing implementation such
as Windows 95 or X Windows.

<P>
Once you create the toolkit, you can initiate a print job via getPrintJob,
which causes a window to pop up on the screen asking the user for information
about which printer is being used, the number of copies to print, and so on.
This process is similar to printing in conventional Windows applications.

<P>
Given a PrintJob object, you can obtain a graphics context and draw to it.
Such drawing, using normal graphics primitives such as fillOval, goes to the
printer instead of the screen. Calling dispose on the graphics context object
sends the page to the printer, and the print job is terminated by calling end.

<P>
If you're using your own custom AWT components, you can use the paint methods
you define for printing without change--by passing them the graphics context
returned by getGraphics. But if you want different behavior when you print,
specialized methods can also be defined for components as necessary.

<P>
<EM>Printing from applets</EM>

<P>
The example given above is a standalone Java program. But what about printing
from applets? The Java security system contains a feature that may lock out
an applet from initiating its own print job, requiring that the initiation be
done via a Web browser or applet viewer. The security area is in a state of
flux at present, and your experiences with this process may be different.

<P>
Finally, another less portable approach to printing text is to open 
the special device file that represents the printer, and do conventional 
file I/O to that file. For example, &quot;lpt1&quot; can be used for 
this purpose with Windows systems, and &quot;/dev/lp&quot; can be used 
with UNIX.


<P>
<CENTER><IMG SRC="/images/chicline.gif" HEIGHT="20" WIDTH="122" alt=""></CENTER>


<P>
<A NAME="tip2"></A>
<STRONG>Global variables</STRONG><BR>
If you've programmed with languages such as C or C++ or Pascal, you've 
probably used global variables and functions. For example, the following 
simple code:

<P>
</FONT>

<PRE>
<CODE>
   int x = 0;

   void f()
   {
      x = 37;
   }
</CODE>
</PRE>

<FONT FACE="Verdana, Arial, Helvetica, sans-serif">

<P>
in C/C++ establishes a global variable &quot;x,&quot; and the function f sets 
its value to 37.

<P>
Java does not have such variables and functions, however. Instead, you wrap
the code in class declarations. The Java equivalent of the code given above
is:

<P>
</FONT>

<PRE>
<CODE>
   public class Globals {
      public static int x = 0;

      public static void f() {x = 37;}
   }
</CODE>
</PRE>

<FONT FACE="Verdana, Arial, Helvetica, sans-serif">

<P>
And you refer to &quot;x&quot; with:

<P>
</FONT>

<PRE>
<CODE>
   Globals.x = 59;
</CODE>
</PRE>

<FONT FACE="Verdana, Arial, Helvetica, sans-serif">

<P>
Note that the &quot;static&quot; keyword is used in declaring these class 
members. This usage makes the members into class variables and methods, that 
is, ones that do not reference or operate on specific class object instances. 
You could say that the class structure in this example is most valuable for 
packaging purposes--instead of as a basis for object-oriented design.

<P>
A common idiom is to use a Java class to group related constants as in the
following example:

<P>
</FONT>

<PRE>
<CODE>
public class TextConstants {
  public static final int WIDTH = 80;
  public static final int HEIGHT = 25;
  public static final int POINTSIZE = 12;
  public static final String FONT = &quot;monospaced&quot;;
}
</CODE>
</PRE>

<FONT FACE="Verdana, Arial, Helvetica, sans-serif">

<P>
You can refer to individual constants with expressions such as:

<P>
</FONT>

<PRE>
<CODE>
   int i = TextConstants.POINTSIZE;
</CODE>
</PRE>

<FONT FACE="Verdana, Arial, Helvetica, sans-serif">

<P>
<EM>Declaring a private constructor</EM>

<P>
There's one more useful tip that applies to this approach. If you're using a
class simply for packaging purposes, it really doesn't make sense to create a
new instance of the class. Instead, you can declare a private constructor:

<P>
</FONT>

<PRE>
<CODE>
   private TextConstants() {}
</CODE>
</PRE>

<FONT FACE="Verdana, Arial, Helvetica, sans-serif">

<P>
The constructor must execute when an instance of the class is created, but
with a private constructor, this is impossible.

</FONT>
</TD>
</TR>
</TABLE>




<!-- ================ -->
<!-- End Main Content -->
<!-- ================ -->

</TD>
</TR>
</TABLE>

<!-- Copyright Insert -->

<BR CLEAR="ALL">

<FORM ACTION="/cgi-bin/search.cgi" METHOD="POST">
<TABLE WIDTH="100%" CELLPADDING="0" BORDER="0" CELLSPACING="5">   
  <TR>
    <TD VALIGN="BOTTOM">


</TD>
</TR>
<A HREF="/servlet/PrintPageServlet"><IMG SRC="/images/printbutton.gif" WIDTH="155" HEIGHT="25" ALT="Print Button" BORDER="0"></A>
	
    <CENTER>
    <FONT SIZE="-1" COLOR="#999999" FACE="Verdana, Arial, Helvetica, sans-serif">
    [ This page was updated: <!-- new date --> 21-Sep-2000 ]
</font></CENTER>
    </TD>
  </TR>
  
  <TR>
    <TD BGCOLOR="#CCCCCC">
    <IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD>
  </TR>
  
  <TR>
    <TD>
    <CENTER>
    <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">
    <A HREF="http://java.sun.com/products/">Products &amp; APIs</A> | 
    <A HREF="/developer/index.html">Developer Connection</A> | 
    <A HREF="/developer/infodocs/index.shtml">Docs &amp; Training</A> | 
    <A HREF="/developer/support/index.html">Online Support</A><BR>
    <A HREF="/developer/community/index.html">Community Discussion</A> |
    <A HREF="http://java.sun.com/industry/">Industry News</A> | 
    <A HREF="http://java.sun.com/solutions">Solutions Marketplace</A> | 
    <A HREF="http://java.sun.com/casestudies">Case Studies</A>
    </FONT>
    </CENTER>
    </TD>
  </TR>
  
  <TR>
    <TD BGCOLOR="#CCCCCC">
    <IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD>
  </TR>

<!--  removed per Gregg Chirlin
  <TR>
    <TD ALIGN="CENTER">
    <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">
    <A HREF="http://java.sun.com/docs/glossary.html">Glossary</A> - 
    <A HREF="http://java.sun.com/applets/">Applets</A> - 
    <A HREF="http://java.sun.com/docs/books/tutorial/">Tutorial</A> - 
    EF="http://java.sun.com/docs/glossary.html">Glossary</A> -
    <A HREF="http://java.sun.com/nav/business/">Business &amp; Licensing</A> - 
    <A HREF="http://java.sun.com/javastore/">Java Store</A> -
    <A HREF="http://java.sun.com/casestudies/">Java in the Real World</A>
    </FONT>
    </TD>
  </TR>
-->

  <TR>
    <TD>
    <CENTER>
    <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">
    <A HREF="http://java.sun.com/docs/glossary.html">Glossary</A> |
    <a href="/feedback/index.html">Feedback</a> | 
    <A HREF="http://java.sun.com/a-z/index.html">A-Z Index</A>
    </FONT>
    </CENTER>

    </TD>
  </TR>
  
  <TR>
    <TD>

    <TABLE WIDTH="100%" CELLPADDING="0" BORDER="0" CELLSPACING="0">
      <TR>
        <TD WIDTH="50%">
        <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">
        For more information on Java technology<BR>
        and other software from Sun Microsystems, call:<BR>
        </FONT>
        <FONT SIZE="-1" FACE="Verdana, Arial, Helvetica, sans-serif">
        (800) 786-7638<BR></FONT>
        <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">
        Outside the U.S. and Canada, dial your country's 
        <A HREF="http://www.att.com/tollfree/international/dialguide/">AT&amp;T&nbsp;Direct&nbsp;Access&nbsp;Number</A> first.<BR>
        </FONT>
        </TD>

        <TD ALIGN="RIGHT" WIDTH="50%">
        <A HREF="http://www.sun.com"><IMG SRC="/images/lgsun.gif" width="64" height="30" border="0" ALT="Sun Microsystems, Inc."></A><BR>
        <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif">
        Copyright &copy; 1995-2000
        <A HREF="http://www.sun.com">Sun Microsystems, Inc.</A><BR>
        All Rights Reserved. 
        <A HREF="http://www.sun.com/share/text/termsofuse.html">Terms of Use</A>. 
        <A HREF="http://www.sun.com/privacy/">Privacy&nbsp;Policy</A>.
        </FONT>
        </TD>
      </TR>
    </TABLE>
	
    </TD>
  </TR> 
</TABLE>
</FORM>

<!-- End Copyright Insert -->


</BODY>
</HTML>
